package com.example.readbook.Adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.readbook.Bean.BookBean
import com.example.readbook.R
import kotlinx.android.synthetic.main.item_book_scanning.view.*

/**
 * Package    : com.example.readbook.Adapter
 * ClassName  : BookScanningAdapter
 * Description: ${TODO}
 * Date       : 2019/3/22 14:58
 */
class BookScanningAdapter(val context: Context, var mList: MutableList<BookBean>) : BaseAdapter<BookBean>(mList) {
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyHolder {
        return RecyHolder(LayoutInflater.from(context).inflate(R.layout.item_book_scanning, p0, false))
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: RecyHolder, p1: Int) {
        val bean = mList[p1]
        holder.itemView.Item_BookScann_bookName.text = bean.bookName
        holder.itemView.Item_BookScann_bookSize.text = bean.size
        Log.e("yxs", "位置$p1 -- 状态：${bean.isChoose}")
        holder.itemView.Item_BookScann_chk.isChecked = bean.isChoose
        holder.itemView.Item_BookScann_Gen.setOnClickListener {
            updateItem(p1, bean.also {
                 val state = it.isChoose
                it.isChoose = !state
                holder.itemView.Item_BookScann_chk.isChecked = it.isChoose
            })
        }
        holder.itemView.Item_BookScann_chk.setOnClickListener {
            updateItem(p1, bean.also {
                val state = it.isChoose
                it.isChoose = !state
                holder.itemView.Item_BookScann_chk.isChecked = it.isChoose
            })
        }
    }

    fun chooseAll(boolean: Boolean) {
        mList.forEach {
            it.isChoose = boolean
        }
        notifyDataSetChanged()
    }
}